package com.hust.bridgecheck.controller.BridgeSensor;

import com.hust.bridgecheck.entity.Bridge;
import com.hust.bridgecheck.entity.DailySensorData;
import com.hust.bridgecheck.entity.Domain;
import com.hust.bridgecheck.entity.Sensor;
import com.hust.bridgecheck.service.BridgeSensor.BridgeService;
import com.hust.bridgecheck.service.BridgeSensor.SensorService;
import com.hust.bridgecheck.service.UserManagement.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;
import java.util.List;

@Controller
@RequestMapping("bridge")
public class BridgeSensorPageController {
    //引入Service
    @Autowired
    private BridgeService bridgeService;
    @Autowired
    private SensorService sensorService;
    @Autowired
    private DomainService domainService;

    @GetMapping("/overview")
    public String overview(Model model) {
        //获取所有的桥和所有的地区，显示在页面上
        List<Object> b_lists = bridgeService.getAllBridge();
        List<Domain> d_lists = domainService.getAllDomain();
        model.addAttribute("selected_index",-1);//选取的domainID
        model.addAttribute("bris",b_lists);
        model.addAttribute("doms",d_lists);
        return "qiaoinformation";
    }

    @GetMapping("/viewdomain/{DID}")
    public String viewBridge(Model model, @PathVariable("DID") Integer domainID)
    {
        List<Object> b_lists = bridgeService.getDomainBridge(domainID);
        List<Domain> d_lists = domainService.getAllDomain();
        model.addAttribute("selected_index",domainID);
        model.addAttribute("bris",b_lists);
        model.addAttribute("doms",d_lists);
        return "qiaoinformation";
    }

    @GetMapping("/searchbridge/{Bname}")
    public String searchBridge(Model model, @PathVariable("Bname") String Bname){
        //功能同上
        List<Object> b_lists = bridgeService.getBridgeByName(Bname);
        List<Domain> d_lists = domainService.getAllDomain();
        model.addAttribute("selected_index",-1);
        model.addAttribute("bris",b_lists);
        model.addAttribute("doms",d_lists);
        return "qiaoinformation";
    }

    @GetMapping("/detail/{BID}")
    public String detail(Model model, @PathVariable("BID") Integer bridgeID){
        //增加权限控制
        //根据ID获取桥和传感器信息
        Bridge currentBridge = bridgeService.getBridgeByID(bridgeID);
        //根据桥ID获取传感器信息
        List<Sensor> s_lists = sensorService.getBridgeSenors(bridgeID);
        model.addAttribute("bridge",currentBridge);
        model.addAttribute("sensors",s_lists);
        return "qiaodetail";
    }

    @GetMapping("/analysis/{BID}")
    public String analysis(Model model, @PathVariable("BID") Integer bridgeID){
        //根据ID获取桥和传感器信息
        Bridge currentBridge = bridgeService.getBridgeByID(bridgeID);
        model.addAttribute("bridge",currentBridge);
        return "data";
    }

    @GetMapping("/getallreport/{BID}/{StartTime}/{EndTime}")
    public String getReportByBridge(Model model,@PathVariable("BID") Integer bridgeID, @PathVariable("StartTime") String Startime, @PathVariable("EndTime") String Endtime){
        Bridge currentBridge = bridgeService.getBridgeByID(bridgeID);
        model.addAttribute("bridge",currentBridge);
        return "detaildata";
    }
}
